package switchviz.core.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.gui.component.IListener;
import switchviz.gui.component.JXMLComboBox;
import switchviz.gui.component.PaddingPanel;
import switchviz.util.FileOperations;

public class ViewTreePanel extends PaddingPanel
{
	/**
	 * 
	 */
	private static Logger logger = Logger.getLogger(ViewTreePanel.class);
	private static final long serialVersionUID = -6089238855884719358L;
	private ViewTree _viewtree = null;
	
	private JButton _buttnew = null;
	private JButton _buttcopy = null;
	private JButton _buttdelete = null;
	private JXMLComboBox _cmbview = null;
	
	public ViewTree getViewTree()
	{
		if (_viewtree == null)
		{
			try
			{
				_viewtree = new ViewTree(ViewTree.getFirstConfiguration(), new ViewXMLFilter());
				_viewtree.getTree().setSelectionRow(0);
			}
			catch (Exception e) { logger.error(e.getMessage()); }
		}
		return _viewtree;
	}
	
	public JComboBox getComboBox()
	{
		return _cmbview.getComboBox();
	}
	
	
	public ViewTreePanel() throws JDOMException, IOException
	{
		if (logger.isInfoEnabled()) { logger.info("Initialize ..."); }
		initialize();
	}
	
	public void addListener(IListener l)
	{
		_buttnew.addActionListener(l);
		_buttcopy.addActionListener(l);
		_buttdelete.addActionListener(l);
		_cmbview.getComboBox().addActionListener(l);
		getViewTree().getTree().addTreeSelectionListener(l);
		getViewTree().getTree().addMouseListener(l);
	}
	
	public void reload() throws Exception
	{
		_cmbview.reload();
		getViewTree().reload();
		getViewTree().getTree().expandRow(0);
		getViewTree().getTree().setSelectionRow(0);
	}
	
	public ViewTree loadNewView(File file)
	{
		try 
		{
			_viewtree.openXML(file); 
			_viewtree.getTree().setSelectionRow(0);
		}
		catch (Exception e) { logger.error(e.getMessage()); }
		return _viewtree;
	}
	
	public void createNewView()
	{
		_cmbview.addItem(ViewTree.createNewView());
	}
	
	public void copyView()
	{
		ViewDataModel datamodel = (ViewDataModel)getComboBox().getSelectedItem();
		String name = datamodel.getName() + "_copy";
		File file = FileOperations.getUniqueXMLInDir(ResourceManager.getInstance().getViewFolder(), name);
		getViewTree().addNode(ViewTree.VIEWNAMENODE, name);
		getViewTree().saveXMLAs(file);
	}
	
	private void initialize() throws JDOMException, IOException
	{
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		_cmbview = new JXMLComboBox("", JXMLComboBox.VIEWLIST, new Dimension(0, 0));
		_cmbview.getComboBox().setActionCommand(ResourceManager.getInstance().getActionSelect());
		_cmbview.getComboBox().setSelectedItem("");

		_buttnew = new JButton("new View", ResourceManager.getInstance().getImageIconNew());
		_buttnew.setActionCommand(ResourceManager.getInstance().getActionNew());
		_buttnew.setToolTipText("Create a new configuration.");
		
		_buttcopy = new JButton("copy View", ResourceManager.getInstance().getImageIconCopy());
		_buttcopy.setActionCommand(ResourceManager.getInstance().getActionCopy());
		_buttcopy.setToolTipText("Copy the current configuration in a new file.");
		
		_buttdelete = new JButton("delete View", ResourceManager.getInstance().getImageIconDelete());
		_buttdelete.setActionCommand(ResourceManager.getInstance().getActionDelete());
		_buttdelete.setToolTipText("Delete the current configuration.");
		
		JPanel buttonpanel = new JPanel();
		buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
		buttonpanel.add(_buttnew);
		buttonpanel.add(_buttcopy);
		buttonpanel.add(_buttdelete);
		
		PaddingPanel editviewpanel = new PaddingPanel();
		editviewpanel.setLayout(new BoxLayout(editviewpanel, BoxLayout.PAGE_AXIS));
		editviewpanel.add(_cmbview);
		editviewpanel.add(buttonpanel);
		
		//this.add(_cmbview, BorderLayout.NORTH);
		this.add(editviewpanel, BorderLayout.NORTH);
		this.add(getViewTree(), BorderLayout.CENTER);
	}
	

}
